/*
 * Command.java
 *
 * Created on January 20, 2007, 8:01 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ca.uvic.group2.testHarness;
import java.util.*;
import java.net.*;

/**
 *
 * @author pat
 */
public class Command {
    
    private String name, username;
    private LinkedList attributes;
    private int commandNumber;
    
    private final String[] loginAttr = {"username", "password"};
    private final String[] commAttr = {"stock", "amount"};
    
    /** Creates a new instance of Command */
    public Command(String line) {
        //Extracting of Command Number
        String tempString = line.substring(line.indexOf("[")+1, line.indexOf("]"));
        commandNumber = Integer.parseInt(tempString);
        
        //Setting up tokenizer to avoid the command number
        String tempLine = line.substring(line.indexOf(" ")+1);
        StringTokenizer lineToken = new StringTokenizer(tempLine, ",");
        
        //Retreving command, username of user, and attributes
        name = (lineToken.nextToken()).trim();
        if(name.equals("ADD")) {
            name = "ADDFUNDS";
        } else if(name.equals("COMMIT_BUY")) {
            name = "COMMIT";
        } else if(name.equals("CANCEL_BUY")) {
            name = "CANCELBUY";
        } else if(name.equals("COMMIT_SELL")) {
            name = "COMMITSELL";
        } else if(name.equals("CANCEL_SELL")) {
            name = "CANCELSELL";
        } else if(name.equals("SET_BUY_AMOUNT")) {
            name = "BUYTRIGGER";
        } else if(name.equals("CANCEL_SET_BUY")) {
            name = "CANCELBUYTRIGGER";
        } else if(name.equals("SET_BUY_TRIGGER")) {
            name = "COMMITBUYTRIGGER";
        } else if(name.equals("SET_SELL_AMOUNT")) {
            name = "COMMITBUYTRIGGER";
        } else if(name.equals("SET_SELL_TRIGGER")) {
            name = "COMMITBUYTRIGGER";
        } else if(name.equals("CANCEL_SET_SELL")) {
            name = "COMMITBUYTRIGGER";
        }
        username = (lineToken.nextToken()).trim();
        attributes = new LinkedList();
        
        //Puts attributes into a LinkedList
        while(lineToken.hasMoreTokens()) {
            attributes.add(lineToken.nextToken().trim());
        }//endWhile
    }//endMethod
    
    public String getName() {
        return name;
    }//endMethod
    
    public String getUsername() {
        return username;
    }//endMethod
    
    public LinkedList getAttributes() {
        return attributes;
    }//endMethod
    
    public int getCommandNumber() {
        return commandNumber;
    }//endMethod
    
    public String getEncoding() {
        String[] attrname;
        int count=0;
        
        if(getName().compareTo("LOGIN") == 0) {
            attrname = loginAttr;
        } else if(getName().compareTo("ADDFUNDS") == 0) {
            count++;
            attrname = commAttr;
        } else {
            attrname = commAttr;
        }
        
        try {
            String data = URLEncoder.encode("cmd", "UTF-8") + "=" + URLEncoder.encode(getName().toLowerCase(), "UTF-8");
            Iterator i = getAttributes().iterator();
            if(getName().compareTo("LOGIN") == 0) {
                data += "&" + URLEncoder.encode(attrname[count++], "UTF-8") + "=" + URLEncoder.encode((String) getUsername(), "UTF-8");
            }
            while(i.hasNext()) {
                data += "&" + URLEncoder.encode(attrname[count++], "UTF-8") + "=" + URLEncoder.encode((String) i.next(), "UTF-8");
            }
            return data;
        } catch(Exception e) {
            return null;
        }
    }
    
}//endClass
